package LineCode.other.thread;

/**
 * @author senselyan
 * @Description: ${todo}
 * @date 2018/3/26 10:27
 */
public class PrintABCABCA_ {

    public static void main(String[] args) {

        Print_ play = new Print_();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    play.print_A();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    play.print_B();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    play.print_C();
                }
            }
        }).start();
    }

    static class Print_ {

        private Integer flag = 0;

        public synchronized void print_A () {
            print_X(0, "A", 1);
        }

        public synchronized void print_B () {
            print_X(1, "B", 2);
        }

        public synchronized void print_C () {
            print_X(2, "C", 0);
        }

        public void print_X (Integer flagx, String x, Integer after) {
            while (flag != flagx) {
                try { this.wait();
                } catch (InterruptedException e) {e.printStackTrace();}
            }
            System.out.print(x);
            flag = after;
            //this.notify(); // 随机输出 AB、ABC  、ABC*10(极少)
            notifyAll();
        }

    }

}